/*  
 *  ------ [ZB_06] - get RSSI from last received packet  -------- 
 *  
 *  Explanation: This program shows how to get RSSI value from the 
 *  last received packet. For this protocol, there is an API function which
 *  shows this information. Before running this example, make sure there 
 *  is another emitter sending packets to this XBee module in order to 
 *  receive information.
 *
 *  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Yuri Carmona
 */



// variable to store RSSI
#include <MakeSureXBeeZB.h>
int rssi;


void setup()
{  
  // init Serial port
  Serial.begin(9600);
  Serial.println(F("ZB_06 example"));

  //////////////////////////
  // 1. init XBee
  //////////////////////////
  xbeeZB.ON();
  
  delay(3000);
    
  //////////////////////////
  // 2. check XBee's network parameters
  //////////////////////////
  checkNetworkParams();
}


void loop()
{

  // check available data in RX buffer
  if( xbeeZB.available() > 0 ) 
  {
    // parse information
    xbeeZB.treatData(); 

    // check RX flag after 'treatData'
    if( !xbeeZB.error_RX ) 
    {
      // check available packets
      while( xbeeZB.pos>0 )
      {  
        Serial.println("\n\nNew packet received:"); 
        Serial.println("---------------------------");

        // get RSSI signal and make conversion to -dBm
        xbeeZB.getRSSI();  
        if( !xbeeZB.error_AT )
        {
          rssi=xbeeZB.valueRSSI[0];
          rssi*=-1;
          Serial.print("RSSI(dBm): ");
          Serial.println(rssi,DEC);
        }   

        // free memory
        free(xbeeZB.packet_finished[xbeeZB.pos-1]);
        xbeeZB.packet_finished[xbeeZB.pos-1]=NULL; 

        // decrement the received packet counter
        xbeeZB.pos--; 
      }
    } 
  }

  delay(5000);
}




/*******************************************
 *
 *  checkNetworkParams - Check operating
 *  network parameters in the XBee module
 *
 *******************************************/
void checkNetworkParams()
{
  // 1. get operating 64-b PAN ID
  xbeeZB.getOperating64PAN();

  // 2. wait for association indication
  xbeeZB.getAssociationIndication();
 
  while( xbeeZB.associationIndication != 0 )
  { 
    delay(2000);
    
    // get operating 64-b PAN ID
    xbeeZB.getOperating64PAN();

    Serial.print(F("operating 64-b PAN ID: "));
    Serial.print(xbeeZB.operating64PAN[0],HEX);
    Serial.print(xbeeZB.operating64PAN[1],HEX);
    Serial.print(xbeeZB.operating64PAN[2],HEX);
    Serial.print(xbeeZB.operating64PAN[3],HEX);
    Serial.print(xbeeZB.operating64PAN[4],HEX);
    Serial.print(xbeeZB.operating64PAN[5],HEX);
    Serial.print(xbeeZB.operating64PAN[6],HEX);
    Serial.print(xbeeZB.operating64PAN[7],HEX);
    Serial.println();     
    
    xbeeZB.getAssociationIndication();
  }

  Serial.println(F("\nJoined a network!"));

  // 3. get network parameters 
  xbeeZB.getOperating16PAN();
  xbeeZB.getOperating64PAN();
  xbeeZB.getChannel();

  Serial.print(F("operating 16-b PAN ID: "));
  Serial.print(xbeeZB.operating16PAN[0],HEX);
  Serial.print(xbeeZB.operating16PAN[1],HEX);
  Serial.println();

  Serial.print(F("operating 64-b PAN ID: "));
  Serial.print(xbeeZB.operating64PAN[0],HEX);
  Serial.print(xbeeZB.operating64PAN[1],HEX);
  Serial.print(xbeeZB.operating64PAN[2],HEX);
  Serial.print(xbeeZB.operating64PAN[3],HEX);
  Serial.print(xbeeZB.operating64PAN[4],HEX);
  Serial.print(xbeeZB.operating64PAN[5],HEX);
  Serial.print(xbeeZB.operating64PAN[6],HEX);
  Serial.print(xbeeZB.operating64PAN[7],HEX);
  Serial.println();

  Serial.print(F("channel: "));
  Serial.print(xbeeZB.channel,HEX);
  Serial.println();

}




